#!/bin/bash
. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/email.sh

# is_store:
# 	if set this param equal "yes" when submit job, this host info will upload to es'index(hosts) automatically.
# 	like:
# 	- is_store: "yes"

get_memory()
{
	memory_total=$(cat /proc/meminfo |grep MemTotal | awk '{print $2}')
	memory_new=$(awk 'BEGIN{printf "%0.2f", '$memory_total'/1024/1024/16}')
	if [ `expr $memory_new \> 16` -eq 1 ]; then
		memory=$(echo `expr $(echo $memory_new | awk '{print int($0)+1}') \* 16`)G
	else
		memory_new=$(awk 'BEGIN{printf "%0.2f", '$memory_total'/1024/1024}')
		memory=$(echo $memory_new | awk '{print int($0)+1}')G
	fi
	echo "memory: $memory"
}

get_cpu()
{
	echo $1: $(lscpu | awk -F': *' '/'$2':/{print $2}')
}

get_cpu_info()
{
	cpu_info=("arch Architecture" "nr_node NUMA\snode\(s\)" "nr_cpu ^CPU\(s\)" "model_name Model\sname")
	for info in "${cpu_info[@]}"
	do
		get_cpu $info
	done
}

print_part()
{
	for i
	do
		echo "  - /dev/disk/by-id/$i"
	done
}

get_rootfs_disk()
{
	nr_hdd_disks=$(lsblk -d -o name,ROTA | grep -v loop | awk '{print $2}' | grep 1 | wc -l)
	nr_ssd_disks=$(lsblk -d -o name,ROTA | grep -v loop | awk '{print $2}' | grep 0 | wc -l)

	# take rootfs_disk from hdd or ssd, hdd is preferentially used as rootfs_disk
	if [[ "$nr_hdd_disks" -eq 0 ]]; then
		rootfs_disk=${ssd_partitions%% *}
		hdd_partitions=
		if [[ "$nr_ssd_disks" -eq 1 ]]; then
			ssd_partitions=
		elif [[ "$nr_ssd_disks" -gt 1 ]]; then
			echo "nr_ssd_partitions: $(( nr_ssd_disks - 1 ))"
			ssd_partitions=${ssd_partitions#* }
		fi
	elif [[ "$nr_hdd_disks" -eq 1 ]]; then
		rootfs_disk=$hdd_partitions
		hdd_partitions=
		[ "$nr_ssd_disks" -ne 0 ] && echo "nr_ssd_partitions: $nr_ssd_disks"
	elif [[ "$nr_hdd_disks" -gt 1 ]]; then
		echo "nr_hdd_partitions: $(( nr_hdd_disks - 1 ))"
		rootfs_disk=${hdd_partitions%% *}
		hdd_partitions=${hdd_partitions#* }
		[ "$nr_ssd_disks" -ne 0 ] && echo "nr_ssd_partitions: $nr_ssd_disks"
	fi

	[ -n "$hdd_partitions" ] && echo "hdd_partitions:" && print_part $hdd_partitions
	[ -n "$ssd_partitions" ] && echo "ssd_partitions:" && print_part $ssd_partitions
	[ -n "$rootfs_disk" ]    && echo "rootfs_disk:"    && print_part $rootfs_disk
}

get_mac()
{
	echo "mac_addr:"
	for i in $(cat /sys/class/net/e*/address)
	do
		echo "  - $i"
	done
}

get_part()
{
	local partitions=

	for i
	do
		[[ "$i" =~ "^" ]] && continue
		# ls -l /dev/disk/by-id/
		info=$(ls -l /dev/disk/by-id/ | grep "/$i" | awk '{print $9}' | grep -e "^scsi-" -e "^ata-" -e "^nvme-" | sed 's/-part[0-9]//g')
		partition=$(echo "$info" | uniq)
		if [ -n "$partitions" ]; then
			partitions="$partitions $partition"
		else
			partitions="$partition"
		fi
	done

	echo "$partitions"
 }

get_partitions()
{
	# lsblk -d -o name,ROTA
	# NAME ROTA
	# sda     0
	# sdb     0
	# sdc     0
	# sdd     0
	# sde     1
	# sdf     1
	ssd=$(lsblk -d -o name,ROTA |grep 0$ | awk '{print $1}')
	hdd=$(lsblk -d -o name,ROTA |grep 1$ | awk '{print $1}')
	if [ -n "$hdd" ]; then
		hdd_partitions=$(get_part $hdd)
	fi

	if [ -n "$ssd" ]; then
		ssd_partitions=$(get_part $ssd)
	fi
}

get_ipmi_ip()
{
	# root@localhost ~# ipmitool lan print
	# Set in Progress         : Set Complete
	# IP Address Source       : Static Address
	# IP Address              : 9.3.4.1
	# Subnet Mask             : 255.255.0.0
	# MAC Address             : e0:00:84:2b:51:7b
	# SNMP Community String   : TrapAdmin12#$
	# IP Header               : TTL=0x40 Flags=0x40 Precedence=0x00 TOS=0x10
	# Default Gateway IP      : 9.3.0.1
	# 802.1q VLAN ID          : Disabled
	# RMCP+ Cipher Suites     : 0,1,2,3,17
	# Cipher Suite Priv Max   : XuuaXXXXXXXXXXX
	[ -e /dev/ipmi0 ] || [ -e /dev/ipmi/0 ] || [ -e /dev/ipmidev/0 ] || return 0
	local ip=$(ipmitool lan print | awk 'NR==3{print $4}')
	[ -n "$ip" ] && echo "ipmi_ip: $ip"
}

get_ipmi_serial_number()
{
	[ -e /dev/ipmi0 ] || [ -e /dev/ipmi/0 ] || [ -e /dev/ipmidev/0 ] || return 0
	local serial_number=$(ipmitool fru | awk '/Product Serial/{print $4}' | head -1)
	[ -n "$serial_number" ] && echo "serial_number: $serial_number"
}

send_mail()
{
	[ "$is_send_mail" = "true" ] || return 0

	recipient_email_to="$secrets_my_email"
	send_email_encode host_info
}

main()
{
	get_memory
	get_partitions
	get_rootfs_disk
	get_mac
	get_cpu_info
	get_ipmi_ip
	get_ipmi_serial_number

	send_mail
}

main
